#include <math.h>
#include <cstdlib>
#include "learnclasses.hpp"
#include "classifyclasses.hpp"
#include "vectclasses.hpp"

void LearningMethodCCM::learn(VectorArray & vectors, VectorArray & classes)
{

    int winner;
    int length = vectors.getVectLength();
    int vectnum = vectors.getVectNumber();
    int classnum = classes.getVectNumber();
	double alpha = myAlpha->getNextValue();
	double step = myStep->getNextValue();

    Vector newvector(length);

	for (int i = 0; i < classnum; i++)
	{
		for (int j = 0; j < length; j++)
		{
			classes.at(i, j) = 1/sqrt((double)length);
		}
	}

	while ((alpha < 1) && (step > 0))
	{
        for (int i = 0; i < vectnum; i++)
        {
            for (int j = 0; j < length; j++)
            {
                newvector.at(j) = alpha*(vectors.at(i, j) + (1 - alpha)/sqrt((double)length));
            }
            winner = Classify::classifyOne(newvector, classes);
            for (int j = 0; j < length; j++)
            {
                classes.at(winner, j) += step*(vectors.at(i, j) - classes.at(winner, j));
            }
            (classes.getVector(winner)).normalize();
        }
        alpha = myAlpha->getNextValue();
		step = myStep->getNextValue();
	}
}

void LearningMethodSimple::learn(VectorArray & vectors, VectorArray & classes)
{
    int winner;
    int length = vectors.getVectLength();
    int vectnum = vectors.getVectNumber();
    int classnum = classes.getVectNumber();
	double step = myStep->getNextValue();

	for (int i = 0; i < classnum; i++)
	{
		for (int j = 0; j < length; j++)
		{
			classes.at(i, j) = rand()%2;
		}
	}
	while (step > 0.1)
	{
        for (int i = 0; i < vectnum; i++)
        {
            winner = Classify::classifyOne(vectors.getVector(i), classes);
            for (int j = 0; j < length; j++)
            {
                classes.at(winner, j) += step*(vectors.at(i, j) - classes.at(winner, j));
            }
        }
        step = myStep->getNextValue();
	}
}
